package com.ndhutu.ee.util.netease.im;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ndhutu.ee.util.StringUtil;
import com.ndhutu.ee.util.netease.App;
import com.ndhutu.ee.util.netease.NeteaseUtil;
import com.ndhutu.ee.util.netease.call.CallUtil;
import okhttp3.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网易云信-即时通讯工具类
 * @Author Shane
 * @Date 2019/10/1616:21
 **/
public class IMUtil {

    /**
     * 创建用户
     * @Author Shane
     * @Date 2019/10/1617:10
     * @param
     * @return
     **/
    public static void create(String accid, String name, String icon, String token, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/create.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);
        params.put("name", name);
        params.put("icon", icon);
        params.put("token", token);

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("网易IM账号创建失败");
        }
    }

    /**
     * 获取用户信息
     * @Author Shane
     * @Date 2020/1/15 15:21
     * @param
     * @return
     **/
    public static Map<String, Object> getUinfos(String accid, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/getUinfos.action";

        Map<String, Object> params = new HashMap<>();

        List<String> userIds = new ArrayList<String>();
        userIds.add(accid);
        params.put("accids", JSONArray.toJSONString(userIds));

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("网易IM获取用户信息失败");
        }

        return ((List<Map<String, Object>>) map.get("uinfos")).get(0);
    }

    /**
     * 更新token
     * @Author Shane
     * @Date 2019/10/17 0:35
     * @param
     * @return
     */
    public static void update(String accid, String token, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/update.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);
        params.put("token", token);

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("网易账号token更新失败");
        }
    }

    /**
     * 封禁用户
     * @Author Shane
     * @Date 2019/10/1617:13
     * @param
     * @return
     **/
    public static void block(String accid, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/block.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);
        params.put("needkick", "true");

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("网易账号封禁失败");
        }
    }

    /**
     * 解封用户
     * @Author Shane
     * @Date 2019/10/1617:13
     * @param
     * @return
     **/
    public static void unblock(String accid, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/unblock.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("网易账号解禁失败");
        }
    }

    /**
     * 更新用户信息
     * @Author Shane
     * @Date 2019/10/17 0:45
     * @param gender 性别 0-女  1-男
     * @return
     */
    public static void updateUinfo(String accid, String name, String icon, Integer gender, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/updateUinfo.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);
        if(!StringUtil.isEmpty(name)){
            params.put("name", name);
        }
        if(!StringUtil.isEmpty(icon)){
            params.put("icon", icon);
        }

        // 用户性别，0表示未知，1表示男，2女表示女，其它会报参数错误
        if(gender != null){
            if(gender == 0){
                params.put("gender", 2);
            }else if(gender == 1){
                params.put("gender", 1);
            }
        }

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("网易账号更新用户信息失败");
        }
    }

    /**
     * 设置黑名单/静音
     * @Author Shane
     * @Date 2019/11/6 16:01
     * @param accid    用户表示
     * @param toUserId  被加黑或加静音的帐号
     * @param value 操作值，0:取消黑名单或静音，1:加入黑名单或静音
     * @param relationType  本次操作的关系类型,1:黑名单操作，2:静音列表操作
     * @return
     **/
    public static void setSpecialRelation(String accid, Integer toUserId, Integer value, Integer relationType, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/setSpecialRelation.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);
        params.put("targetAcc", toUserId+"");
        params.put("relationType", relationType+"");
        params.put("value", value+"");

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("设置黑名单/静音失败");
        }
    }

    /**
     * 账号全局禁用音视频
     * @Author Shane
     * @Date 2020/3/14 22:56
     * @param mute  是否全局禁言：true：全局禁言，false:取消全局禁言
     * @return
     */
    public static void muteAv(String accid, Boolean mute, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/user/muteAv.action";

        Map<String, Object> params = new HashMap<>();
        params.put("accid", accid);
        params.put("mute", mute+"");

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("账号全局禁用音视频失败");
        }
    }

    /**
     * 消息撤回
     * @Author Shane
     * @Date 2020/6/17 16:24
     * @param msgid     要撤回消息的msgid
     * @param timetag   要撤回消息的创建时间
     * @param from      发消息的accid
     * @param to        接收消息的accid
     * @return
     */
    public static void recall(String msgid, long timetag, String from, String to, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/msg/recall.action";

        Map<String, Object> params = new HashMap<>();
        params.put("deleteMsgid", msgid);
        params.put("timetag", timetag);
        params.put("type", 7);    //点对点消息撤回
        params.put("from", from);
        params.put("to", to);
        params.put("ignoreTime", "1");  //绕过时间检查

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("消息撤回失败");
        }
    }

    /**
     * 发送消息
     * @Author Shane
     * @Date 2020/7/13 22:50
     * @param
     * @return
     */
    public static Map<String, Object> sendMsg(String from, String to, String msg, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/msg/sendMsg.action";

        Map<String, Object> params = new HashMap<>();
        params.put("from", from);
        params.put("ope", 0);   //点对点个人消息
        params.put("to", to);
        params.put("type", 0);  //文本消息

        Map<String, Object> body = new HashMap<>();
        body.put("msg", msg);
        params.put("body", JSONObject.toJSONString(body));

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("发送消息失败");
        }else{
            return (Map<String, Object>) map.get("data");
        }
    }

    /**
     * 发送消息
     * @Author Shane
     * @Date 2020/7/13 22:50
     * @param
     * @return
     */
    public static Map<String, Object> sendMsg(String from, String to, Integer type, Map<String, Object> body, App app) throws Exception{
        String url = "https://api.netease.im/nimserver/msg/sendMsg.action";

        Map<String, Object> params = new HashMap<>();
        params.put("from", from);
        params.put("ope", 0);   //点对点个人消息
        params.put("to", to);
        params.put("type", type);  //文本消息

        params.put("body", JSONObject.toJSONString(body));

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = NeteaseUtil.post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            throw new Exception("发送消息失败");
        }else{
            return (Map<String, Object>) map.get("data");
        }
    }

    public static void main(String[] args) throws Exception{
//        App app = new App("f31308a092ca2a9afaa474af066c1935", "d0c8bd355963");
//        deleteRoom("52693668497361", app);

    }

}
